package com.ruoyi.web.controller.laiqianba.source;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.db.domain.SourceMemberDataInfoTemp;
import com.ruoyi.laiqianba.service.IMemberSourceDictInfoService;
import com.ruoyi.web.wrapper.SourceMemberDataInfoTempWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.SourceMemberDataInfoHistory;
import com.ruoyi.source.service.ISourceMemberDataInfoHistoryService;
import com.ruoyi.web.wrapper.SourceMemberDataInfoHistoryWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 线索用户资质信息Controller
 * 
 * @author ruoyi
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/source/history")
public class SourceMemberDataInfoHistoryController extends BaseController
{
    @Autowired
    private ISourceMemberDataInfoHistoryService sourceMemberDataInfoHistoryService;

    @Autowired
    private IMemberSourceDictInfoService memberSourceDictInfoService;

    /**
     * 查询线索用户资质信息列表
     */
    @PreAuthorize("@ss.hasPermi('source:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        startPage();
        List<Map<String, Object>> list = sourceMemberDataInfoHistoryService.selectSourceMemberDataInfoHistoryMapList(sourceMemberDataInfoHistory);
        Map<String, String> sourceDictMap = memberSourceDictInfoService.selectSourceDictMap();
        list = new SourceMemberDataInfoTempWrapper(list).setSourceDictMap(sourceDictMap).wrap();
        return getDataTable(list);
    }

    /**
     * 导出线索用户资质信息列表
     */
    @PreAuthorize("@ss.hasPermi('source:history:export')")
    @Log(title = "线索用户资质信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        List<SourceMemberDataInfoHistory> list = sourceMemberDataInfoHistoryService.selectSourceMemberDataInfoHistoryList(sourceMemberDataInfoHistory);
        ExcelUtil<SourceMemberDataInfoHistory> util = new ExcelUtil<SourceMemberDataInfoHistory>(SourceMemberDataInfoHistory.class);
        util.exportExcel(response, list, "线索用户资质信息数据");
    }

    @GetMapping("/city")
    public AjaxResult getCity(SourceMemberDataInfoHistory sourceMemberDataInfoHistory) {

        JSONObject returnData = new JSONObject();
        if(StrUtil.equals(sourceMemberDataInfoHistory.getType(),"1")){
            returnData.put("cityList", sourceMemberDataInfoHistoryService.getCity(sourceMemberDataInfoHistory));
        } else {
            returnData.put("cityList", sourceMemberDataInfoHistoryService.getCityList(sourceMemberDataInfoHistory));
        }
        return AjaxResult.success(returnData);
    }

    /**
     * 获取线索用户资质信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('source:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sourceMemberDataInfoHistoryService.selectSourceMemberDataInfoHistoryById(id));
    }

    /**
     * 新增线索用户资质信息
     */
    @PreAuthorize("@ss.hasPermi('source:history:add')")
    @Log(title = "线索用户资质信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        return toAjax(sourceMemberDataInfoHistoryService.insertSourceMemberDataInfoHistory(sourceMemberDataInfoHistory));
    }

    /**
     * 修改线索用户资质信息
     */
    @PreAuthorize("@ss.hasPermi('source:history:edit')")
    @Log(title = "线索用户资质信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SourceMemberDataInfoHistory sourceMemberDataInfoHistory)
    {
        return toAjax(sourceMemberDataInfoHistoryService.updateSourceMemberDataInfoHistory(sourceMemberDataInfoHistory));
    }

    /**
     * 删除线索用户资质信息
     */
    @PreAuthorize("@ss.hasPermi('source:history:remove')")
    @Log(title = "线索用户资质信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sourceMemberDataInfoHistoryService.deleteSourceMemberDataInfoHistoryByIds(ids));
    }
}
